import { createRouter, createWebHistory } from 'vue-router'
// router/index.ts
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import WelcomeHub from '@/views/WelcomeHub.vue'
import { Toast } from 'vant'
import 'vant/es/toast/style'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'WelcomeHub',
      component: WelcomeHub
    },
    {
      path: '/Login',
      name: 'Login',
      component: () => import('@/views/Login.vue')
    },
    {
      path: '/Register',
      name: 'Register',
      component: () => import('@/views/Register.vue')
    },
    {
      path: '/editUserInfo',
      component: () => import('@/views/User/components/editUserInfo.vue'),
      meta: { title: '编辑个人信息' }
    },
    {
      path: '/safeCenter',
      component: () => import('@/views/User/components/safeCenter.vue'),
      meta: { title: '安全中心' }
    },
    {
      path: '/updateAccount',
      component: () => import('@/views/User/components/updateAccount.vue'),
      meta: { title: '修改手机号' }
    },
    {
      path: '/updatePassword',
      component: () => import('@/views/User/components/updatePassword.vue'),
      meta: { title: '修改密码' }
    },
    {
      path: '/set',
      component: () => import('@/views/User/Set.vue'),
      meta: { title: '设置' }
    },
    {
      path: '/customerService',
      component: () => import('@/views/User/customerService.vue'),
      meta: { title: '客服' }
    },
    {
      path: '/admin',
      component: () => import('@/views/Admin/index.vue'),
      meta: { title: '系统后台' }
    },
    {
      path: '/consultant-admin',
      component: () => import('@/views/Consultant/index.vue'),
      meta: { title: '咨询师入口' }
    },
    {
      path: '/editConsultantInfo',
      component: () => import('@/views/Consultant/components/editConsultantInfo.vue'),
      meta: { title: '编辑咨询师个人信息' }
    },
    {
      path: '/cancelAppointment',
      component: () => import('@/views/Consultant/components/cancelAppointment.vue'),
      meta: { title: '核销预约' }
    },
    {
      path: '/scheduling',
      component: () => import('@/views/Consultant/components/scheduling.vue'),
      meta: { title: '预约排期设置' }
    },
    {
      path: '/appointmentHistory',
      component: () => import('@/views/Consultant/components/appointmentHistory.vue'),
      meta: { title: '用户预约记录' }
    },
    {
      path: '/consultantRoomList',
      component: () => import('@/views/Consultant/components/consultantRoomList.vue'),
      meta: { title: '咨询师聊天室列表' }
    },
    {
      path: '/addArticle',
      component: () => import('@/views/Consultant/components/addArticle.vue'),
      meta: { title: '添加文章页面' }
    },
    {
      path: '/editArticle/:id',
      component: () => import('@/views/Consultant/components/editArticle.vue'),
      meta: { title: '编辑文章页面' }
    },
    {
      path: '/consultantRoom/:id',
      component: () => import('@/views/Consultant/components/consultantRoom.vue'),
      meta: { title: '咨询师聊天室' }
    },
    {
      path: '/articlePublish',
      component: () => import('@/views/Consultant/components/articlePublish.vue'),
      meta: { title: '文章发布' }
    },
    {
      path: '/detail/:id',
      name: 'detail',
      component: () => import('@/views/Article/detail.vue'),
      meta: { title: '详情' }
    },
    {
      path: '/articleStore',
      name: 'articleStore',
      component: () => import('@/views/User/components/articleStore.vue'),
      meta: { title: '收藏列表' }
    },
    {
      path: '/readHistory',
      name: 'readHistory',
      component: () => import('@/views/User/readHistory.vue'),
      meta: { title: '阅读历史' }
    },
    {
      path: '/booking',
      name: 'booking',
      component: () => import('@/views/Home/appointment/booking.vue'),
      meta: { title: '快速预约' }
    },
    {
      path: '/appointment/bookingHistory',
      name: 'bookingHistory',
      component: () => import('@/views/Home/appointment/bookingHistory.vue'),
      meta: { title: '我的预约' }
    },
    {
      path: '/consult/list',
      name: 'consultList',
      component: () => import('@/views/Home/consult/consultList.vue'),
      meta: { title: '聊天通道列表' }
    },
    {
      path: '/consultRoom/:id',
      name: 'consultRoom',
      component: () => import('@/views/Home/consult/consultRoom.vue'),
      meta: { title: '预约聊天室' }
    },
    {
      path: '/consult/fast',
      name: 'consultFast',
      component: () => import('@/views/Home/consult/consultFast.vue'),
      meta: { title: '咨询' }
    },
    {
      path: '/other/heartPaper',
      name: 'heartPaper',
      component: () => import('@/views/Home/other/heartPaper.vue'),
      meta: { title: '心灵纸条' }
    },
    {
      path: '/other/illnessInquiry',
      name: 'illnessInquiry',
      component: () => import('@/views/Home/other/illnessInquiry.vue'),
      meta: { title: '疾病查询' }
    },
    {
      path: '/other/notice',
      name: 'notice',
      component: () => import('@/views/Home/other/notice.vue'),
      meta: { title: '系统公告' }
    },
    {
      path: '/other/nearBy',
      name: 'nearBy',
      component: () => import('@/views/Home/other/nearBy.vue'),
      meta: { title: '附近心灵驿站' }
    },
    {
      path: '/bookingCounselor/:id',
      name: 'bookingCounselor',
      component: () => import('@/views/Home/appointment/bookingCounselor.vue'),
      meta: { title: '咨询师列表' }
    },
    {
      path: '/detailConsultant/:id',
      name: 'detailConsultantr',
      component: () => import('@/views/Home/appointment/detailConsultant.vue'),
      meta: { title: '咨询师详情页' }
    },
    {
      path: '/admin_notice',
      name: 'admin_notice',
      component: () => import('@/views/Admin/components/admin-notice.vue'),
      meta: { title: '公告管理' }
    },
    {
      path: '/admin_consultants',
      name: 'admin_consultants',
      component: () => import('@/views/Admin/components/admin-consultants.vue'),
      meta: { title: '咨询师管理' }
    },
    {
      path: '/admin_appoint',
      name: 'admin_appoint',
      component: () => import('@/views/Admin/components/admin-appoint.vue'),
      meta: { title: '预约管理' }
    },
    {
      path: '/setTimeSlots',
      name: 'setTimeSlots',
      component: () => import('@/views/Admin/components/setTimeSlots.vue'),
      meta: { title: '预约时段设置' }
    },
    {
      path: '/admin_users',
      name: 'admin_users',
      component: () => import('@/views/Admin/components/admin-users.vue'),
      meta: { title: '用户管理' }
    },
    {
      path: '/addNotice',
      name: 'addNotice',
      component: () => import('@/views/Admin/components/addNotice.vue'),
      meta: { title: '添加公告' }
    },
    {
      path: '/addConsultant',
      name: 'addConsultant',
      component: () => import('@/views/Admin/components/addConsultant.vue'),
      meta: { title: '添加咨询师' }
    },
    {
      path: '/index',
      redirect: '/home',
      component: () => import('@/views/Layout/index.vue'),
      //四个子路由
      children: [
        {
          path: '/home',
          component: () => import('@/views/Home/index.vue'),
          meta: { title: '首页' }
        },
        {
          path: '/article',
          component: () => import('@/views/Article/index.vue'),
          name: 'atricle',
          meta: { title: '心灵文摘', keepAlive: true }
        },
        {
          path: '/notify',
          component: () => import(/* webpackChunkName: "article" */ '@/views/Notify/index.vue'),
          meta: { title: '消息通知' }
        },
        {
          path: '/user',
          component: () => import('@/views/User/index.vue'),
          meta: { title: '个人中心' }
        },
        {
          path: '/test',
          component: () => import('@/views/Test.vue'),
          meta: { title: '测试页面' }
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  let arr = ['/user', '/article']
  if (arr.includes(to.path)) {
    //校验是否登录
    const isLogin = localStorage.getItem('tokenStr')
    if (!isLogin) {
      Toast.fail('请先登录')
      next('/login')
      return
    }
  }

  NProgress.start() // 进度条开始
  next()
})

router.afterEach(() => {
  NProgress.done() // 进度条结束
})
// router/index.ts

NProgress.configure({
  easing: 'ease', // 动画方式
  speed: 500, // 递增进度条的速度
  showSpinner: false, // 是否显示加载 icon
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3 // 初始化时的最小百分比
})

export default router
